package com.xh.core.handler;

import com.aspose.words.*;

/*
 * @Author daiwei
 * @Description
 * @Date 2019/12/24 11:53
 **/
public class WordReplaceWithDocEvaluator implements IReplacingCallback {

    public WordReplaceWithDocEvaluator(final FindReplaceOptions options, Document doc) {
        mOptions = options;
        this.doc = doc;
    }

    /**
     * NOTE: This is a simplistic method that will only work well when the match
     * starts at the beginning of a run.
     */
    @Override
    public int replacing(final ReplacingArgs e) throws Exception {
        DocumentBuilder builder = new DocumentBuilder((Document) e.getMatchNode().getDocument());
        builder.moveTo(e.getMatchNode());

        // Replace '<CustomerName>' text with a red bold name.
        //builder.insertNode(doc);
//        builder.insertHtml(doc.getText());
        builder.insertDocument(doc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
        e.setReplacement("");
        //   NodeCollection childNodes = e.getMatchNode().getParentNode().getChildNodes(NodeType.NULL, true);
        return ReplaceAction.REPLACE;
    }

    private FindReplaceOptions mOptions;
    private Document doc;
}